﻿using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;

public static class Status {

    // increase/decrease population on end of turn
    // rating

    public enum Indices
    {
        Money, Infrastructure, Health, Education, Security, Corruption, Satisfaction, NPopulation
    }

    public static int year, month;
    private static float[] indices, yearRating;
    public static List<Effect> effectsInPlay = new List<Effect>();

    public static void Start()
    {
        year = month = 1;
        yearRating = new float[12];
        indices = new float[Enum.GetValues(typeof(Indices)).Length];
        
        SetIndice(Indices.Infrastructure, UnityEngine.Random.Range(60.0f, 80.0f));
        SetIndice(Indices.Health, UnityEngine.Random.Range(60.0f, 80.0f));
        SetIndice(Indices.Education, UnityEngine.Random.Range(60.0f, 80.0f));
        SetIndice(Indices.Security, UnityEngine.Random.Range(60.0f, 80.0f));

        SetIndice(Indices.NPopulation, UnityEngine.Random.Range(15000, 20000));
        SetIndice(Indices.Corruption, 10.0f);
        SetIndice(Indices.Satisfaction, 50.0f);
        
        SetIndice(Indices.Money, AnnualTaxes());
    }

    public static float AnnualTaxes()
    {
        return (int)(GetIndice(Indices.NPopulation) * 0.05f);
    }
    public static float MonthlyTaxes()
    {
        return (int)(GetIndice(Indices.NPopulation) * 0.05f / 12.0f);
    }

    public static float GetIndice(Indices ind)
    {
        return indices[(int)ind];
    }
    public static void SetIndice(Indices ind, float val)
    {
        if(ind == Indices.NPopulation)
            indices[(int)ind] = Mathf.Clamp(val, 0, 100000);
        else if (ind == Indices.Money)
            indices[(int)ind] = Mathf.Clamp(val, 0, 9999);
        else
            indices[(int)ind] = Mathf.Clamp(val, 0, 100);            
    }
    public static void AddIndice(Indices ind, float val)
    {
        if (ind == Indices.NPopulation)
            indices[(int)ind] = Mathf.Clamp(val + indices[(int)ind], 0, 100000);
        else if (ind == Indices.Money)
            indices[(int)ind] = Mathf.Clamp(val + indices[(int)ind], 0, 9999);
        else
            indices[(int)ind] = Mathf.Clamp(val + indices[(int)ind], 0, 100);   
    }

    public static void CallEndOfTurn()
    {
        int indLength = Enum.GetValues(typeof(Indices)).Length;
        for (int i = 0; i < effectsInPlay.Count; i++)
        {
            for (int j = 0; j < indLength; j++)
            {
                AddIndice((Indices)j, effectsInPlay[i].effect[j]);
            }
            if (--(effectsInPlay[i].turnsInEffect) <= 0)
            {
                effectsInPlay.RemoveAt(i--);
            }
        }
        AddIndice(Indices.Money, MonthlyTaxes());

        float rating = 0;

        
        int nlower = 0;
        float aux = GetIndice(Indices.Infrastructure);
        rating += (aux - 50);
        if (aux < 50) nlower++;

        aux = GetIndice(Indices.Health);
        rating += (aux - 50);
        if (aux < 50) nlower++;

        aux = GetIndice(Indices.Education);
        rating += (aux - 50);
        if (aux < 50) nlower++;

        aux = GetIndice(Indices.Security);
        rating += (aux - 50);
        if (aux < 50) nlower++;

        if (nlower > 1 && rating > 0)
            rating = 0;
        rating = Mathf.Clamp(rating/2 + GetIndice(Indices.Satisfaction) / (GetIndice(Indices.Corruption)+1),0,100);

        yearRating[month++ - 1] = rating;
                
        AddIndice(Indices.NPopulation, (rating - 50) * 20);
        if (month > 12)
        {
            year++;
            month = 1;
            AddIndice(Indices.Money, AnnualTaxes());
            
            float average = 0;
            foreach (float f in yearRating)
            {
                average += f;
            }
        
            average /= yearRating.Length;

            if (average < 50) // numeros mágicos presentes
                Application.LoadLevel("GameOver");
            else if (year >= 10)
                Application.LoadLevel("Victory");
        }
    }
}
